<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>HELLO Index</h1>

    <script>
      const baseURL = "http://127.0.0.1:8090/api/collections";

      const xhr = new XMLHttpRequest();
      xhr.open("GET", `${baseURL}/todolist/records`);
      // 设置报头
      xhr.setRequestHeader("username", "jack");
      // age: 20, 21
      xhr.setRequestHeader("age", 20);
      xhr.setRequestHeader("age", 21);

      xhr.responseType = "json";

      /*
      content-length: 1397
      content-type: application/json; charset=UTF-8
      date: Thu, 04 Jul 2024 06:35:28 GMT
      vary: Origin
      x-content-type-options: nosniff
      x-frame-options: SAMEORIGIN
      x-xss-protection: 1; mode=block
      */
      // 成功响应 200 ok
      xhr.onload = function () {
        console.log("content-length", xhr.getResponseHeader("content-length"));
        console.log("content-type", xhr.getResponseHeader("content-type"));
        console.log(xhr.getAllResponseHeaders());
        console.log(
          Object.fromEntries(
            xhr
              .getAllResponseHeaders()
              .split("\r\n")
              .slice(0, -1)
              .map((el) => el.split(": "))
          )
        );

        console.log(
          xhr
            .getAllResponseHeaders()
            .split("\r\n")
            .slice(0, -1)
            .reduce((r, el) => {
              const [k, v] = el.split(": ");
              r[k] = v;
              return r;
            }, {})
        );
        // console.log(xhr.response);
      };
      xhr.send();
    </script>
  </body>
</html>
